package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.entities.AgentBalance;

public class AgentBalanceDAOImpls extends DAO<AgentBalance> implements
		AgentBalanceDAO {

	public AgentBalanceDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "AgentBalance";
	}

	@Override
	public void create(AgentBalance entity) throws SQLException {
		query = "insert into AgentBalance "
				+ " ( agentbalance_id, agent_id, typestatus, currencyid, openingbalance, currentbalance) values(?, ?, ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getAgentbalance_id());
		pstmt.setLong(i++, entity.getAgent_id());
		pstmt.setString(i++, entity.getTypestatus());
		pstmt.setLong(i++, entity.getCurrencyid());
		pstmt.setDouble(i++, entity.getOpeningbalance());
		pstmt.setDouble(i++, entity.getCurrentbalance());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(AgentBalance entity) throws SQLException {
		query = "update "
				+ table_name
				+ " set agent_id = ?, typestatus = ?, currencyid = ?, openingbalance= ?, currentbalance = ?"
				+ "where agentbalance_id = " + entity.getAgentbalance_id();
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getAgent_id());
		pstmt.setString(i++, entity.getTypestatus());
		pstmt.setLong(i++, entity.getCurrencyid());
		pstmt.setDouble(i++, entity.getOpeningbalance());
		pstmt.setDouble(i++, entity.getCurrentbalance());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery() + " where agent_id = " + key;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	protected AgentBalance readRecord(ResultSet rs) throws SQLException {
		AgentBalance l_entity = new AgentBalance();
		l_entity.setAgentbalance_id(rs.getLong("agentbalance_id"));
		l_entity.setAgent_id(rs.getLong("agent_id"));
		l_entity.setTypestatus(rs.getString("typestatus"));
		l_entity.setCurrencyid(rs.getLong("currencyid"));
		l_entity.setOpeningbalance(rs.getDouble("openingbalance"));
		l_entity.setCurrentbalance(rs.getDouble("currentbalance"));
		return l_entity;
	}

	@Override
	public AgentBalance find(long key) throws SQLException {
		AgentBalance l_AgentBalance = null;
		query = getSelectQuery() + " where agent_id = " + key;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_AgentBalance = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_AgentBalance;
	}

	@Override
	public AgentBalance getInfo(AgentBalance info) throws SQLException {
		AgentBalance l_AgentBalance = null;
		query = getSelectQuery() + " where agent_Id = " + info.getAgent_id() + " and "
				+ "currencyId = " + info.getCurrencyid();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if (rs.next())
			l_AgentBalance = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_AgentBalance;
	}
}
